<?php

namespace app\admin\controller;


class System extends Common
{
    /**
     * 系统设置
     * @return mixed
     * @throws \think\Exception
     */
    function system(){
        if(request()->isPost()){
            $data=request()->post();
            $data['am_delivery_time']=$data['am_delivery_time1'].'-'.$data['am_delivery_time2'];
            $data['pm_delivery_time']=$data['pm_delivery_time1'].'-'.$data['pm_delivery_time2'];
            unset($data['am_delivery_time1']);
            unset($data['am_delivery_time2']);
            unset($data['pm_delivery_time1']);
            unset($data['pm_delivery_time2']);
            unset($data['uploadfile']);
            $file=request()->file('user_head_img');
            if($file){
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    echo '<h1 style="color: red;">头像修改失败，请重试；</h1>';
                }else{
                    $img=$return['url'];
                }
                $data['user_head_img']=$img;
            }else{
                unset($data['user_head_img']);
            }
            //用户默认头像
            $file=request()->file('start_img');
            if($file){
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    echo '<h1 style="color: red;">启动页修改失败，请重试；</h1>';
                }else{
                    $img=$return['url'];
                }
                $data['start_img']=$img;
            }else{
                unset($data['start_img']);
            }
            //启动页图片
            $file=request()->file('goods_img');
            if($file){
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    echo '<h1 style="color: red;">默认商品展示图片修改失败，请重试；</h1>';
                }else{
                    $img=$return['url'];
                }
                $data['goods_img']=$img;
            }else{
                unset($data['goods_img']);
            }
            //商品展示图片
            $file=request()->file('goods_image');
            if($file){
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    echo '<h1 style="color: red;">商品默认轮播图修改失败，请重试；</h1>';
                }else{
                    $img=$return['url'];
                }
                $data['goods_image']=$img;
            }else{
                unset($data['goods_image']);
            }
            //商品轮播图片
            $file=request()->file('type_img');
            if($file){
                $upload = new Upload();
                $return=$upload->upload($file);
                if($return['code']!=1){
                    echo '<h1 style="color: red;">头像修改失败，请重试；</h1>';
                }else{
                    $img=$return['url'];
                }
                $data['type_img']=$img;
            }else{
                unset($data['type_img']);
            }
            //分类默认图片
            $res=db('system')
                ->where(array('id'=>1))
                ->update($data);
            if($res!==false){
                $system=db('system')
                    ->where(array('id'=>1))
                    ->find();
                $am_delivery_time=explode('-',$system['am_delivery_time']);
                $am_delivery_time1=$am_delivery_time[0];
                $am_delivery_time2=$am_delivery_time[1];
                $pm_delivery_time=explode('-',$system['pm_delivery_time']);
                $pm_delivery_time1=$pm_delivery_time[0];
                $pm_delivery_time2=$pm_delivery_time[1];
                $this->assign('system', $system);
                $this->assign('am_delivery_time1', $am_delivery_time1);
                $this->assign('am_delivery_time2', $am_delivery_time2);
                $this->assign('pm_delivery_time1', $pm_delivery_time1);
                $this->assign('pm_delivery_time2', $pm_delivery_time2);
                return $this->fetch();
            }else{
                echo '<h1 style="color: red;">修改出错，请重试；</h1>';
            }
        }else{
            $system=db('system')
                ->where(array('id'=>1))
                ->find();
            $am_delivery_time=explode('-',$system['am_delivery_time']);
            $am_delivery_time1=$am_delivery_time[0];
            $am_delivery_time2=$am_delivery_time[1];
            $pm_delivery_time=explode('-',$system['pm_delivery_time']);
            $pm_delivery_time1=$pm_delivery_time[0];
            $pm_delivery_time2=$pm_delivery_time[1];
            $this->assign('system', $system);
            $this->assign('am_delivery_time1', $am_delivery_time1);
            $this->assign('am_delivery_time2', $am_delivery_time2);
            $this->assign('pm_delivery_time1', $pm_delivery_time1);
            $this->assign('pm_delivery_time2', $pm_delivery_time2);
            return $this->fetch();
        }
    }
    /**
     * 自提地址列表
     * @return mixed
     * @throws \think\Exception
     */
    public function address_list()
    {
        $where['a.pa_id'] = array('gt', 0);

        $page = Request()->param('page');
        $size = Request()->param('size');
        //分页
        $name = Request()->param('name');
        //名称
        $mobile = Request()->param('mobile');
        //电话
        $status = Request()->param('status');
        //状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        if ($status > 0) {
            if ($status == 1) {
                $where['a.pa_status'] = 1;
            } else {
                $where['a.pa_status'] = array('neq', 1);
            }
        }
        if ($name != '') {
            $where['a.pa_name'] = array('like', '%' . $name . '%');
        }
        if ($mobile != '') {
            $where['a.pa_mobile'] = array('like', '%' . $mobile . '%');
        }

        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.pa_create_time'] = $where_time;
        $now = ($page - 1) * $size;
        $data = db('pickup_address a')
            ->where($where)
            ->limit("$now,$size")
            ->order("a.pa_sort desc,a.pa_status")
            ->select();
        $count = db('pickup_address a')
            ->where($where)
            ->count();
        $page_str = page($page, $size, $count, count($data));//分页
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('status', $status);
        $this->assign('name', $name);
        $this->assign('mobile', $mobile);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('page_str', $page_str);
        return $this->fetch();
    }

    //禁用自提地址
    function address_del()
    {
        if (request()->isAjax()) {
            $id = request()->post('id');
            $res = db('pickup_address')
                ->where(array('pa_id' => $id))
                ->update(array('pa_status' => 2, 'pa_update_time' => time()));
            if ($res !== false) {
                return return_json(array('code' => 1, 'msg' => '禁用成功'), '禁用自提地址');
            } else {
                return return_json(array('code' => 0, 'msg' => '禁用失败'), '禁用自提地址');
            }
        }
    }

    //启用自提地址
    function address_use()
    {
        if (request()->isAjax()) {
            $id = request()->post('id');
            $res = db('pickup_address')
                ->where(array('pa_id' => $id))
                ->update(array('pa_status' => 1, 'pa_update_time' => time()));
            if ($res !== false) {
                return return_json(array('code' => 1, 'msg' => '启用成功'), '启用自提地址');
            } else {
                return return_json(array('code' => 0, 'msg' => '启用失败'), '启用自提地址');
            }
        }
    }

    //添加自提地址
    function add_address()
    {
        if (request()->isGet()) {
            return $this->fetch();
        } else if (request()->isAjax()) {
            $data = request()->post();
            if($data['pa_name']==''){
                return return_json(array('code' => 0, 'msg' => '请输入自提人员名称'), '添加自提地址');
            }
            if($data['pa_mobile']==''){
                return return_json(array('code' => 0, 'msg' => '请输入自提人员电话'), '添加自提地址');
            }
            if($data['pa_address']==''){
                return return_json(array('code' => 0, 'msg' => '请输入自提地址'), '添加自提地址');
            }
            if($data['pa_sort']<=0){
                $data['pa_sort']=0;
            }
            $data['pa_create_time']=time();
            $data['pa_add_user_id']=session('uid');
            $data['pa_add_user_name']=session('user.real_name');
            $res=db('pickup_address')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'自提地址添加成功'),'添加自提地址');
            }else{
                return return_json(array('code'=>0,'msg'=>'自提地址添加失败'),'添加自提地址');
            }
        }

    }
    //协议、文章列表
    /**
     * 自提地址列表
     * @return mixed
     * @throws \think\Exception
     */
    public function article_list()
    {
        $where['a.id'] = array('gt', 0);

        $page = Request()->param('page');
        $size = Request()->param('size');
        //分页
        $name = Request()->param('name');
        //名称
        $number = Request()->param('number');
        //编号
        $status = Request()->param('status');
        //状态
        $start_time = strtotime(Request()->param('start_time'));
        $end_time = strtotime(Request()->param('end_time'));
        if ($page < 1) {
            $page = 1;
        }
        if ($size < 15) {
            $size = 15;
        }
        if ($status > 0) {
            if ($status == 1) {
                $where['a.status'] = 1;
            } else {
                $where['a.status'] = array('neq', 1);
            }
        }
        if ($name != '') {
            $where['a.title'] = array('like', '%' . $name . '%');
        }
        if ($number != '') {
            $where['a.number'] = array('like', '%' . $number . '%');
        }
        $where_time = array(array('gt', 0), array('gt', 0));
        if ($start_time > 0) {
            $where_time[] = array('egt', $start_time);
        } else {
            $start_time = '';
        }
        if ($end_time > 0) {
            $where_time[] = array('elt', $end_time);
        } else {
            $end_time = '';
        }
        $where['a.create_time'] = $where_time;
        $now = ($page - 1) * $size;
        $data = db('article a')
            ->where($where)
            ->limit("$now,$size")
            ->order("a.id desc")
            ->select();
        $count = db('article a')
            ->where($where)
            ->count();
        $page_str = page($page, $size, $count, count($data));//分页
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->assign('page', $page);
        $this->assign('size', $size);
        $this->assign('status', $status);
        $this->assign('name', $name);
        $this->assign('number', $number);
        $this->assign('start_time', $start_time);
        $this->assign('end_time', $end_time);
        $this->assign('page_str', $page_str);
        return $this->fetch();
    }
    //添加协议、文章
    function add_article(){
        if(request()->isGet()){
            return $this->fetch();
        }else if(request()->isAjax()){
            $data=Request()->post();
            $data['title']=trim($data['title']);
            //问题标题
            if($data['title']==''){
                return return_json(array('code'=>0,'msg'=>'标题不能为空'),'添加协议、文章');
            }
            $data['number']=trim($data['number']);
            //问题标题
            if($data['number']==''){
                return return_json(array('code'=>0,'msg'=>'编号不能为空'),'添加协议、文章');
            }
            if(!isset($data['editorValue']) || $data['editorValue']==''){
                return return_json(array('code'=>0,'msg'=>'请输入详细内容'),'添加协议、文章');
            }
            $data['content']=$data['editorValue'];
            unset($data['editorValue']);
            if($data['status']!=1){
                $data['end_time']=time();
            }
            $data['create_time']=time();
            $data['add_user_id']=session('uid');
            $data['add_user_name']=session('user.real_name');
            $res=db('article')->insertGetId($data);
            //添加权限
            if($res>0){
                return return_json(array('code'=>1,'msg'=>'协议、文章添加成功'),'添加协议、文章');
            }else{
                return return_json(array('code'=>0,'msg'=>'协议、文章添加成功'),'添加协议、文章');
            }
        }
    }
    //禁用协议、文章
    function article_del()
    {
        if (request()->isAjax()) {
            $id = request()->post('id');
            $res = db('article')
                ->where(array('id' => $id))
                ->update(array('status' => 2, 'end_time' => time()));
            if ($res !== false) {
                return return_json(array('code' => 1, 'msg' => '禁用成功'), '禁用协议、文章');
            } else {
                return return_json(array('code' => 0, 'msg' => '禁用失败'), '禁用协议、文章');
            }
        }
    }
    //启用协议、文章
    function article_use()
    {
        if (request()->isAjax()) {
            $id = request()->post('id');
            $res = db('article')
                ->where(array('id' => $id))
                ->update(array('status' => 1, 'end_time' => time()));
            if ($res !== false) {
                return return_json(array('code' => 1, 'msg' => '启用成功'), '启用协议、文章');
            } else {
                return return_json(array('code' => 0, 'msg' => '启用失败'), '启用协议、文章');
            }
        }
    }
    //协议文章详情
    function article_details(){
        $id = request()->get('id');
        $article = db('article')
            ->where(array('id' => $id))
            ->find();
        $this->assign('article', $article);
        return $this->fetch();
    }

}